<?php

/**
 * model_notice
 */

class Model_notice extends YOU {

	public function __construct()
	{
        //error_reporting(E_WARNING);
		parent::__construct();

		@$this->model->audit          = YOU::loadModel('audit');
		@$this->model->user           = YOU::loadModel('user');
	}

	/** 读取成配置 **/
	public function getIni()
	{
		$notice_lists = $this->getAll();
		foreach ($notice_lists as $key => $value){
			$notice[$value['notice_id']] = $value['notice_name'];
		}

		return $notice;
	}

	/** 读取列表 **/
	public function getAll($field = "*")
	{
		$notice_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_notice` ");

		return $notice_lists;
	}

	/** 读取信息 **/
	public function getInfo($notice_id, $field = "*")
	{
		$data = $this->db->fetchRow("SELECT {$field} FROM `tbl_notice` WHERE notice_id='{$notice_id}' ");

		return $data;
	}
	/** 读取通知表最后一条信息 **/
	public function getEnd()
	{
		$data = $this->db->fetchRow("SELECT * FROM `tbl_notice` ORDER BY notice_id DESC LIMIT 1");

		return $data;
	}

	/** 部门阅读人 **/
	public function getReadRow($notice_id,$user_id, $field = "*")
	{

		$read_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_notice_read` WHERE notice_id ='{$notice_id}' AND read_to = '{$user_id}'");

		return $read_data;
	}
	/** 每个发布者选中的 部门全部阅读人 **/
	public function getReadAll($notice_id, $field = "*")
	{

		$read_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_notice_read` WHERE notice_id ='{$notice_id}'");

		return $read_lists;
	}
	/** 部门阅读人(参数教师id) **/
	public function getReadInfo($staff_id, $field = "*")
	{

		$read_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_notice_read` WHERE  read_to = '{$staff_id}'");

		return $read_data;
	}
	/** -----------------------------------首页部分------------------------------ **/
	/** 显示公示 **/
	public function getIndexOne($user_id,$notice_type,$num, $field = "*")
	{
		$lits = $this->db->fetchAll("SELECT {$field} FROM `tbl_notice` AS a,`tbl_notice_read` AS b  WHERE a.notice_id=b.notice_id AND  b.read_to ='{$user_id}' AND a.notice_type = '{$notice_type}'  ORDER BY b.is_read ASC,a.notice_id DESC LIMIT 0,{$num}");
	
		foreach($lits  as $key => $value){
			$department_data = $this->db->fetchRow("SELECT * FROM `tbl_department` WHERE department_id={$value['department_id']}");
			$lits[$key]['department_name'] = $department_data['department_name'];
			$lits[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));
		}
		return $lits;
	}
	/** 显示公示总条数 **/
	public function getIndexOneCount($user_id,$notice_type)
	{
		$lits = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_notice` AS a,`tbl_notice_read` AS b  WHERE a.notice_id=b.notice_id AND  b.read_to ='{$user_id}' AND a.notice_type = '{$notice_type}' AND b.is_read = '0'");

		return $lits;
	}
	/** 显示校长室通知（不需要审核） **/
	public function getIndexTwo($user_id, $num, $time, $field = "*")
	{

		$lits = $this->db->fetchAll(" SELECT * FROM `tbl_notice` AS a,
  `tbl_notice_read` AS b WHERE a.notice_id=b.notice_id AND a.notice_type IN (2,3) AND a.is_show IN (1,3) AND b.read_to ='{$user_id}' AND start_time <='{$time}' ORDER BY b.is_read ASC,a.notice_id DESC LIMIT 0,{$num} ");
		foreach($lits  as $key => $value){
			$department_data = $this->db->fetchRow("SELECT * FROM `tbl_department` WHERE department_id={$value['department_id']}");
			$lits[$key]['department_name'] = $department_data['department_name'];
			$lits[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));
		}
		return $lits;
	}
	/** 显示校长室通知总条数（不需要审核） **/
	public function getIndexTowCount($user_id, $time)
	{
		$lits = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_notice` AS a,`tbl_notice_read` AS b WHERE a.notice_id=b.notice_id AND a.notice_type IN (2,3) AND a.is_show IN (1,3) AND b.read_to ='{$user_id}' AND start_time <='{$time}' AND b.is_read= '0' ");
		
		return $lits;
	}

	/** 审核（未审核） **/
	public function getIndexThree($user_id,$notice_type,$num, $field = "*")
	{
	
		// 审核人
		$audit_lists = $this->model->audit->getInfoAll($user_id);

		foreach ($audit_lists as $key=>$value){
			// 部门
			if ($value['audit_type'] == 'department') {
				$row['department'][$key] = $value['type_id'];
			}
			// 教研组
			if ($value['audit_type'] == 'research') {
				$row['research'][$key] = $value['type_id'];
			}
			// 备课组
			if ($value['audit_type'] == 'prepare') {
				$row['prepare'][$key] = $value['type_id'];
			}
			// 学科组
			if ($value['audit_type'] == 'discipline') {
				$row['discipline'][$key] = $value['type_id'];
			}
			// 级长
			if ($value['audit_type'] == 'grade') {
				$row['grade'][$key] = $value['type_id'];
			}
		}
		if (!empty($row)) {
			foreach ($row as $key => $value){
				if($key=='department'){
					$department = implode(',',$value);
				}
				if($key=='research'){
					$research = implode(',',$value);
				}
				if($key=='prepare'){
					$prepare = implode(',',$value);
				}
				if($key=='discipline'){
					$discipline = implode(',',$value);
				}
				if($key=='grade'){
					$grade = implode(',',$value);
				}
			}
		}

		$wheres = " ";
		if (!empty($department)){
			$wheres .= " AND ( audit_type='department' AND type_id IN ($department) AND notice_type = '{$notice_type}')";
		}
		if (!empty($research)){
			$wheres .= " OR ( audit_type='research' AND type_id IN ($research) AND notice_type = '{$notice_type}')";
		}
		if (!empty($prepare)){
			$wheres .= " OR ( audit_type='prepare' AND type_id IN ($prepare) AND notice_type = '{$notice_type}')";
		}
		if (!empty($discipline)){
			$wheres .= " OR ( audit_type='discipline' AND type_id IN ($discipline) AND notice_type = '{$notice_type}')";
		}
		if (!empty($grade)){
			$wheres .= " OR ( audit_type='grade' AND type_id IN ($grade) AND notice_type = '{$notice_type}')";
		}
		if (!empty($audit_lists)) {
			$lits = $this->db->fetchAll("SELECT {$field} FROM `tbl_notice` WHERE notice_type = '{$notice_type}' AND is_show ='0' {$wheres} ORDER BY notice_id DESC LIMIT 0,{$num}");

			foreach($lits  as $key => $value){
				$department_data = $this->db->fetchRow("SELECT * FROM `tbl_department` WHERE department_id={$value['department_id']}");
				$lits[$key]['department_name'] = $department_data['department_name'];
				$lits[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));
			}
		}

		return $lits;
	}
	/** 审核（未审核） **/
	public function getIndexThreeCount($type_id,$notice_type, $field = "*")
	{
		// 审核人
		$audit_lists = $this->model->audit->getInfoAll($type_id);

		foreach ($audit_lists as $key=>$value){
			// 部门
			if ($value['audit_type'] == 'department') {
				$row['department'][$key] = $value['type_id'];
			}
			// 教研组
			if ($value['audit_type'] == 'research') {
				$row['research'][$key] = $value['type_id'];
			}
			// 备课组
			if ($value['audit_type'] == 'prepare') {
				$row['prepare'][$key] = $value['type_id'];
			}
			// 学科组
			if ($value['audit_type'] == 'discipline') {
				$row['discipline'][$key] = $value['type_id'];
			}
			// 级长
			if ($value['audit_type'] == 'grade') {
				$row['grade'][$key] = $value['type_id'];
			}
		}
		if (!empty($row)) {
			foreach ($row as $key => $value){
				if($key=='department'){
					$department = implode(',',$value);
				}
				if($key=='research'){
					$research = implode(',',$value);
				}
				if($key=='prepare'){
					$prepare = implode(',',$value);
				}
				if($key=='discipline'){
					$discipline = implode(',',$value);
				}
				if($key=='grade'){
					$grade = implode(',',$value);
				}
			}
		}


		$wheres = " ";
		if (!empty($department)){
			$wheres .= " AND ( audit_type='department' AND type_id IN ($department) AND notice_type = '{$notice_type}')";
		}
		if (!empty($research)){
			$wheres .= " OR ( audit_type='research' AND type_id IN ($research) AND notice_type = '{$notice_type}')";
		}
		if (!empty($prepare)){
			$wheres .= " OR ( audit_type='prepare' AND type_id IN ($prepare) AND notice_type = '{$notice_type}')";
		}
		if (!empty($discipline)){
			$wheres .= " OR ( audit_type='discipline' AND type_id IN ($discipline) AND notice_type = '{$notice_type}')";
		}
		if (!empty($grade)){
			$wheres .= " OR ( audit_type='grade' AND type_id IN ($grade) AND notice_type = '{$notice_type}')";
		}
		if (!empty($audit_lists)) {
			$lits = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_notice`  WHERE notice_type = '{$notice_type}' AND is_show ='0'  {$wheres}");

		}
		return $lits;
	}
	/** 事务交代 **/
	public function getIndexFour($user_id,$notice_type, $num, $field = "*")
	{

		$lits = $this->db->fetchAll("SELECT DISTINCT a.notice_id as r ,a.*,b.* FROM `tbl_notice` AS a,`tbl_notice_read` AS b WHERE a.notice_id = b.notice_id AND b.read_to = '{$user_id}' AND a.notice_type='{$notice_type}'  ORDER BY b.is_read ASC,a.notice_id DESC LIMIT 0,{$num}");

		foreach($lits as $key => $value){
			// 名字
			$user_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_user` WHERE user_id='{$value['user_id']}' ");
			
			if ($user_data['user_type'] == "staff"){
				$row = $this->db->fetchRow("SELECT staff_name FROM `tbl_staff` WHERE staff_id='{$user_data['type_id']}'");
				$row =$row['staff_name'];
			}
			if ($user_data['user_type'] == "admin"){
				$row = $this->db->fetchRow("SELECT {$field} FROM `tbl_user` WHERE user_id='{$user_id}' ");
				$row=$row['user_name'];
			}
			if ($user_data['user_type'] == "student"){
				$row = $this->db->fetchRow("SELECT student_name FROM `tbl_student` WHERE student_id='{$user_data['type_id']}'");
				$row=$row['student_name'];
			}
			$lits[$key]['department_name'] = $row;	
			// 时间转换
			$lits[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));
		}
		
		return $lits;
	}
	/** 事务交代总条数 **/
	public function getIndexFourCount($user_id,$notice_type, $field = "*")
	{
		$data = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_notice` AS a,`tbl_notice_read` AS b WHERE a.notice_id = b.notice_id AND b.read_to = {$user_id} AND a.notice_type={$notice_type} AND b.is_read='0' ");

		return $data;
	}
	/** 首页提醒 **/
	public function getIndexSix($user_id, $notice_type, $num, $field = "*")
	{
		$lits = $this->db->fetchAll("SELECT DISTINCT a.notice_id as r ,a.*,b.* FROM `tbl_notice` AS a,`tbl_notice_read` AS b WHERE a.notice_id = b.notice_id AND b.read_to = '{$user_id}' AND a.notice_type='{$notice_type}' AND b.is_show ='0'  ORDER BY a.end_time ASC LIMIT 0,{$num}");
		
		foreach($lits as $key => $value){
			// 名字
			$user_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_user` WHERE user_id='{$value['user_id']}' ");
			
			if ($user_data['user_type'] == "staff"){
				$row = $this->db->fetchRow("SELECT staff_name FROM `tbl_staff` WHERE staff_id='{$user_data['type_id']}'");
				$row =$row['staff_name'];
			}
			if ($user_data['user_type'] == "admin"){
				$row = $this->db->fetchRow("SELECT {$field} FROM `tbl_user` WHERE user_id='{$user_id}' ");
				$row=$row['user_name'];
			}
			if ($user_data['user_type'] == "student"){
				$row = $this->db->fetchRow("SELECT student_name FROM `tbl_student` WHERE student_id='{$user_data['type_id']}'");
				$row=$row['student_name'];
			}
			$lits[$key]['department_name'] = $row;	
			// 时间转换
			$lits[$key]['end_time'] =date('m-d H:i',strtotime($value['end_time']));
		}
		return $lits;
	}
	/** 首页提醒(总数) **/
	public function getIndexSixCount($user_id, $notice_type, $num, $field = "*")
	{
		
		$data = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_notice` AS a,`tbl_notice_read` AS b WHERE a.notice_id = b.notice_id AND b.read_to = '{$user_id}' AND a.notice_type='{$notice_type}' AND b.is_show ='0' ");

		return $data;
	}








}



?>